Flutter SDK 插件
Flutter SDK 插件同时具备 无埋点SDK 和 埋点SDK 功能,但是在使用 Flutter 无埋点功能前需要按照 Flutter Aspect 集成 才能使无埋点功能生效。
环境配置
- Flutter SDK 插件的更新日志,可参阅 Release Notes
- Flutter SDK 无埋点如何生效,请阅读 Flutter Aspect 集成
Growingio Flutter SDK 插件集成
添加依赖
以工程flutter_app
为例,在pubspec.yaml
文件中添加依赖。
dependencies:
growingio_flutter_plugin: '4.0.0'
然后执行 flutter pub get
指令安装插件。
点击查看 GrowingIO Flutter 插件和 SDK 版本的依赖关系
Flutter 插件版本 | Android SDK 版本范围 | Apple SDK 版本范围 |
---|---|---|
>= v1.1.3 | >= v3.5.0 | >= v3.5.0 |
= v2.0.0 | >= v4.1.0 | >= v4.1.0 |
若是纯 Flutter 应用,上述集成就可以使用了。若是项目中同时具有原生代码和Flutter,且原生也需要支持无埋点功能,那么就需要基于无埋点初始化配置集成原生的SDK。 Flutter和原生共用一套SDK逻辑,不会初始化两次。
在Android中, Gradle Plugin 若出现 'no growingio autotracker sdk dependency was found' 的编译错误,可以将插件的配置项 skipDependencyCheck 设置为true。
Flutter 插件初始化
GrowingIO Flutter SDK 支持在 Flutter 中初始化 SDK,也同时支持在原生代码中初始化。如果需要更多的功能设置,我们更推荐您在原生端实现初始化。
原生端初始化
原生端初始化请参考各端的初始化文档:
- Android: 无埋点初始化配置、埋点初始化配置,另外,在 Android 原生初始化需要额外添加 Flutter 模块
- iOS: 无埋点初始化配置、埋点初始化配置
Flutter 初始化
在 Flutter 端进行初始化,请将 SDK 的初始化代码放入 main.dart
的 main
中,代码示例如下:
- 无埋点
- 埋点
void main() async {
...
WidgetsFlutterBinding.ensureInitialized();
GrowingAutotracker.startWithConfiguration(
projectId: "Your ProjectId",
dataCollectionServerHost: "Your collection server host",
dataSourceId: "Your datasourceid",
urlScheme: "Yoour Url Scheme",
debugEnabled: true,
modules: {
AdvertLibraryGioModule(config: AdvertConfig()),
},
dataCollectionEnabled: true,
autoTrackAllRoutePage: false,
idMappingEnabled: true,
cellularDataLimit: 10,
dataUploadInterval: 15,
sessionInterval: 20,
);
...
runApp(MyApp());
}
void main() async {
...
WidgetsFlutterBinding.ensureInitialized();
GrowingTracker.startWithConfiguration(
projectId: "Your ProjectId",
dataCollectionServerHost: "Your collection server host",
dataSourceId: "Your datasourceid",
urlScheme: "Yoour Url Scheme",
debugEnabled: true,
modules: {
AdvertLibraryGioModule(config: AdvertConfig()),
ProtobufLibraryModule()
},
dataCollectionEnabled: true,
idMappingEnabled: true,
cellularDataLimit: 10,
dataUploadInterval: 15,
sessionInterval: 20,
);
...
runApp(MyApp());
}
初始化配置说明
在 Flutter 初始化中会传入各式的参数,参数配置如下表所示:
配置项 | 参数类型 | 是否必填 | 默认值 | 说明 | 版本 |
---|---|---|---|---|---|
projectId | String | 是 | null | 项目ID,每个应用对应唯一值 | - |
urlScheme | String | 是 | null | Android 应用特有的URLScheme,用于外部应用拉起应用,如圈选 | - |
dataSourceId | String | 是 | null | 应用的DataSourceId,唯一值 | - |
dataCollectionServerHost | String | 是 | null | 服务端部署后的 ServerHost | - |
autoTrackAllRoutePage | bool | 否 | false | 是否自动开启页面采集,无埋点独有 | >=4.1.0 |
channel | String | 否 | null | 应用的分发渠道 | - |
debugEnabled | bool | 否 | false | 调试模式,会打印SDK log,抛出错误异常,在线上环境请关闭 | - |
cellularDataLimit | int | 否 | 10 | 每天发送数据的流量限制,单位MB | - |
dataUploadInterval | int | 否 | 15 | 数据发送的间隔,单位秒 | - |
sessionInterval | int | 否 | 30 | 会话后台留存时长,单位秒 | - |
dataCollectionEnabled | bool | 否 | true | 是否采集数据 | - |
requestTimeout | int | 否 | 30 | 设置数据上报请求的超时时间,单位秒 | - |
androidIdEnabled | bool | 否 | false | 是否在 Android 设备上采集 AndroidId | - |
imeiEnabled | bool | 否 | false | 是否在 Android 设备上采集 imei | - |
modules | Set<LibraryGioModule> | 否 | empty | 模块集成,具体请阅读下方的模块说明 | - |
urlScheme 说明
在使用 GrowingIO SDK 的Mobile Debugger 和圈选功能时,需要外部浏览器通过扫描二维码来拉起应用。
模块配置
GrowingIO SDK 利用模块来实现SDK核心功能以外的额外功能,在 Flutter SDK 插件中,可以通过在 modules 传入模块声明来开启相应的功能。
目前 Flutter SDK 可启用的模块功能包括:
广告功能
GrowingTracker.startWithConfiguration(
//....
modules: {
AdvertLibraryGioModule(
config: AdvertConfig(readClipBoardEnable: true, /// 是否打开剪切板
deepLinkHost: "Your deepLinkHost", /// 深度链接配置地址, SaaS取默认值 “https://link.growingio.com”
asaEnabled: true, /// 仅iOS端使用
deepLinkCallback:(Map<String,String> params,int error,int time){
///监听深度链接中的地址参数
},
)),
},
//....
);
广告模块包括激活事件和深度链接,能帮助客户提供广告,活动的引导跳转和下载。
在 Flutter SDK 启动广告模块同时,原生端(包括Android和iOS端)都需要引入相应的模块代码,请参考:
加密模块
GrowingTracker.startWithConfiguration(
//....
modules: {
EncoderLibraryGioModule(),
},
//....
);
加密模块用于数据网络上传数据的加密。
Json 模块
GrowingTracker.startWithConfiguration(
//....
modules: {
JsonLibraryModule(),
},
//....
);
Json 数据模块将会使用 Json 格式保存和上传事件数据。
H5混合模块
GrowingTracker.startWithConfiguration(
//....
modules: {
HybridLibraryGioModule(),
},
//....
);
若使用了原生的WebView,且内嵌 H5 页面如果也需要进行数据采集(H5 页面已经集成 Web JS SDK),则可以开启该 H5混合模块。
在 Flutter SDK 启动H5混合模块同时,原生端(包括Android和iOS端)都需要引入相应的模块代码,请参考:
- Android 端 默认已引入H5混合模块
- iOS 端 需引入H5混合模块
API说明
- 无埋点
- 埋点
GrowingAutotracker.get().setDataCollectionEnabled(true)
GrowingAutotracker.get().setLoginUserId(userId: "cpacm",userKey: "name")
GrowingAutotracker.get().cleanLoginUserId()
GrowingAutotracker.get().setLoginUserAttributes(attributes: {"sex":"female"});
GrowingAutotracker.get().setLocation(latitude: 20.11,longitude: 20.11)
GrowingAutotracker.get().cleanLocation()
GrowingAutotracker.get().getDeviceId()
GrowingAutotracker.get().trackCustomEvent(eventName: "eventName", attributes: {"age":"18"})
String? timerId =await GrowingAutotracker.get().trackTimerStart(eventName: "custom");
GrowingAutotracker.get().trackTimerPause(timerId: timerId!);
GrowingAutotracker.get().trackTimerResume(timerId: timerId);
GrowingAutotracker.get().trackTimerEnd(timerId: timerId,attributes: {});
GrowingAutotracker.get().removeTimer(timerId: timerId);
GrowingAutotracker.get().clearTrackTimer();
GrowingAutotracker.get().setGeneralProps(props:{});
GrowingAutotracker.get().removeGeneralProps(keys:["col1 row1", "key1"]);
GrowingAutotracker.get().clearGeneralProps();
GrowingAutotracker.get().registerComponent(module);
GrowingTracker.get().setDataCollectionEnabled(true)
GrowingTracker.get().setLoginUserId(userId: "cpacm",userKey: "name")
GrowingTracker.get().cleanLoginUserId()
GrowingTracker.get().setLocation(latitude: 20.11,longitude: 20.11)
GrowingTracker.get().cleanLocation()
GrowingTracker.get().setLoginUserAttributes(attributes: {"sex":"female"})
GrowingTracker.get().getDeviceId()
GrowingTracker.get().trackCustomEvent(eventName: "custom",attributes: {"item":"exp"});
String? timerId =await GrowingTracker.get().trackTimerStart(eventName: "custom");
GrowingTracker.get().trackTimerPause(timerId: timerId!);
GrowingTracker.get().trackTimerResume(timerId: timerId);
GrowingTracker.get().trackTimerEnd(timerId: timerId,attributes: {});
GrowingTracker.get().removeTimer(timerId: timerId);
GrowingTracker.get().clearTrackTimer();
GrowingTracker.get().setGeneralProps(props:{});
GrowingTracker.get().removeGeneralProps(keys:["col1 row1", "key1"]);
GrowingTracker.get().clearGeneralProps();
GrowingTracker.get().registerComponent(module);
1. 数据采集开关
setDataCollectionEnabled
打开或关闭数据采集
参数说明
参数 | 参数类型 | 说明 |
---|---|---|
enabled | boolean | true 打开数据采集,false 关闭数据采集,默认 true |
示例
- 无埋点
- 埋点
GrowingAutotracker.get().setDataCollectionEnabled(true);
GrowingTracker.get().setDataCollectionEnabled(true);
2. 设置登录用户ID
setLoginUserId
当用户登录之后调用,设置登录用户ID
-
如果您的App每次用户升级版本时无需重新登录的话,为防止用户本地缓存被清除导致的无法被识别为登录用户,建议在用户每次升级App版本后初次访问时重新调用setLoginUserId方法
-
当需要标记用户ID类型时,请先进行规划,并在平台的数据中心,添加新的用户身份类型,再设置userkey,误设会影响数据质量。 同时在初始化 SDK 时设置
idMappingEnabled
为true
参数说明
参数 | 参数类型 | 说明 |
---|---|---|
userId | String | 长度限制大于0且小于等于1000,如果大于长度1000将只截取前1000长度 |
userKey | String | 适用于ID-MAPPING,可设置 userId 的类型,可选填 |
示例
- 无埋点
- 埋点
GrowingAutotracker.get().setLoginUserId(userId: "cpacm",userKey: "name");
GrowingTracker.get().setLoginUserId(userId: "cpacm",userKey: "name");
3. 清除登录用户ID
cleanLoginUserId
当用户登出之后调用,清除已经设置的登录用户ID。
示例
- 无埋点
- 埋点
GrowingAutotracker.get().cleanLoginUserId();
GrowingTracker.get().cleanLoginUserId();